package com.a01_2021.thousandstimes.saas.controller;


import com.a01_2021.thousandstimes.saas.entity.vo.OpenRecruitQuery;
import com.a01_2021.thousandstimes.saas.service.OpenRecruitmentService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/open-recruitment")
public class OpenRecruitmentController {

    @Autowired
    private OpenRecruitmentService openRecruitmentService;

    //HR发布岗位
    @PreAuthorize("hasAuthority('open-recruitment-saveOr')")
    @PostMapping("saveOr")
    @DS("#header.instanceid")
    //@Transactional
    public AjaxResponse saveOr(@RequestBody(required = false) OpenRecruitQuery openRecruitQuery) {
        //try {
        AjaxResponse resultBean = openRecruitmentService.insertOpenRecruitment(openRecruitQuery);
        if (resultBean.getError()=="您已经发布过该岗位"){
            return AjaxResponse.error("您已经发布过该岗位");
        }else{
            openRecruitmentService.insertRecruit(openRecruitQuery);
            return AjaxResponse.success("发布成功");
        }
        //} catch (Exception e) {
        //return AjaxResponse.error("发布失败");
        //}

    }

    //根据岗位状态查询岗位
    @PreAuthorize("hasAuthority('open-recruitment-getOrByState')")
    @DS("master")
    @PostMapping("getOrByState")
    @ResponseBody
    public AjaxResponse getOrByState(Integer state) {
        return openRecruitmentService.getOpenRecruitment(state);
    }

    //根据instance查询岗位
    @PreAuthorize("hasAuthority('open-recruitment-getOrByInstance')")
    @DS("master")
    @PostMapping("getOrByInstance")
    @ResponseBody
    public AjaxResponse getOrByInstance(Long instanceId) {
        return openRecruitmentService.getOpenRecruitmentByInstance(instanceId);
    }

    //根据HR查询岗位
    @PreAuthorize("hasAuthority('open-recruitment-getOrByHr')")
    @DS("master")
    @PostMapping("getOrByHr")
    @ResponseBody
    public AjaxResponse getOrByHr(String hr) {
        return openRecruitmentService.getOpenRecruitmentByHr(hr);
    }


    //HR结束招聘
    @PreAuthorize("hasAuthority('open-recruitment-overOr')")
    @DS("master")
    @PostMapping("overOr")
    public AjaxResponse overOr(Integer id) {

        return openRecruitmentService.setOpenRecruitmentState(id);

    }

}

